﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSNghiPhep : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string ThoiGian { get; set; }
        public void EditNghiPhep(int hdId, DateTime ngayBatDau,DateTime ngayKetThuc, string ghiChu, string lyDo)
        {
            List<NSNghiPhep> lst = (List<NSNghiPhep>)HttpContext.Current.Session["NghiPhep"];
            NSNghiPhep pc = lst.Where(a => a.NghiPhepID.Equals(hdId)).SingleOrDefault();
            pc.ThoiGianBatDau = ngayBatDau;
            pc.ThoiGianKetThuc = ngayKetThuc;
            pc.GhiChu = ghiChu;
            pc.LyDoNghiPhep = lyDo;
           
        }
        public void DeleteNghiPhep(int hdId)
        {
            List<NSNghiPhep> lst = (List<NSNghiPhep>)HttpContext.Current.Session["NghiPhep"];
            NSNghiPhep pc = lst.Where(a => a.NghiPhepID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllNghiPhepByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSNghiPheps
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSNghiPheps.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateNghiPhep_NhanVien(int nhanVienId, List<NSNghiPhep> nblObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSNghiPheps
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSNghiPheps.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSNghiPhep pc in nblObject)
            {
                NSNghiPhep item = new NSNghiPhep();
                CopyNSNghiPhep(pc, item);
                db.NSNghiPheps.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSNghiPhep(NSNghiPhep src, NSNghiPhep des)
        {
            des.LyDoNghiPhep = src.LyDoNghiPhep;
            des.ThoiGianBatDau = src.ThoiGianBatDau;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.ThoiGianKetThuc = src.ThoiGianKetThuc;

        }
        public List<NSNghiPhep> LoadNghiPhepByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSNghiPheps
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.ThoiGianBatDau descending
                         select a).ToList();
            foreach (NSNghiPhep item in query)
            {

                item.ThoiGian = string.Format("{0}-{1}", item.ThoiGianBatDau != null ? ((DateTime)item.ThoiGianBatDau).ToString("dd/MM/yyyy") : string.Empty, item.ThoiGianKetThuc!=null? ((DateTime)item.ThoiGianKetThuc).ToString("dd/MM/yyyy"):string.Empty);
            }
            return query;

        }
        public NSNghiPhep GetNghiPhep(int nhanVienId, DateTime ngayBatDau, DateTime ngayKetThuc, string ghiChu, string lyDo)
        {
            
            var db = new DataClassesDataContext();
            NSNghiPhep hd = new NSNghiPhep();
            hd.NghiPhepID = 0 - DateTime.Now.Second;
            hd.ThoiGianBatDau = ngayBatDau;
            hd.ThoiGianKetThuc = ngayKetThuc;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.LyDoNghiPhep = lyDo;
            hd.ThoiGian= string.Format("{0}-{1}", ngayBatDau.ToString("dd/MM/yyyy"), ngayKetThuc.ToString("dd/MM/yyyy"));
            return hd;
        }
    }
}